<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Order_MxShopPage extends WebPage {

    public function main() {
        global $_W, $_GPC;
        $this -> orderList(1, 'main');
    }
    
    public function ingoing(){
        global $_W, $_GPC;
        $this -> orderList(2, 'ingoing');
    }

    public function finish(){
        global $_W, $_GPC;
        $this -> orderList(3, 'finish');
    }

    public function close(){
        global $_W, $_GPC;
        $this -> orderList(4, 'close');
    }

    public function all(){
        global $_W, $_GPC;
        $this -> orderList(0, 'all');
    }

    public function refund(){
        global $_W, $_GPC;
        
        $this -> orderList(5, 'refund');
    }

    public function refund_finish(){
        global $_W, $_GPC;

        $this -> orderList(6, 'refund_finish');
    }
    
    protected function orderList($status, $action){
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];
        
        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        
        $condition = ' o.deleted=0';

        $paras = array();

        $orderstatus = array(1 => array('css' => 'danger', 'name' => '待付款'), 2 => array('css' => 'warning', 'name' => '进行中'), 3 => array('css' => 'success', 'name' => '已完成'), 4 => array('css' => 'default', 'name' => '已关闭'), 5 => array('css' => 'danger', 'name' => '维权申请'), 6 => array('css' => 'default', 'name' => '维权完成',));
        
        if($action != 'all'){
            $condition .= ' AND o.status = :status';
            $paras[':status'] = $status;
        }

        $paytype = array('3'=>array('name'=>'余额'), '24'=>array('name'=>'微信'), '38'=>array('name'=>'支付宝'));
        
        $type = intval($_GPC['type']);
        if(!empty($type)){
            $condition .= ' AND o.paytype = :paytype';
            $paras[':paytype'] = $type;
        }

        if (empty($starttime) || empty($endtime))
        {
            $starttime = strtotime('-1 month');
            $endtime = time();
        }
        $searchtime = trim($_GPC['searchtime']);
        if (!(empty($searchtime)) && is_array($_GPC['time']) && in_array($searchtime, array('create', 'pay', 'send', 'finish')))
        {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
            $condition .= ' AND o.' . $searchtime . 'time >= :starttime AND o.' . $searchtime . 'time <= :endtime ';
            $paras[':starttime'] = $starttime;
            $paras[':endtime'] = $endtime;
        }
        
        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if($_GPC['searchfield'] == 'ordersn'){
                $condition .= ' AND locate(:keyword,o.ordersn)>0';
            
            } else if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0)';

            } else if ($_GPC['searchfield'] == 'goods'){
                $condition .= ' AND locate(:keyword,o.goods_name)>0 ';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,o.mendian_name)>0 ';
            }
        }

        $list = pdo_fetchall('SELECT o.* ,m.realname as realname, r.reason as reason, r.content as content,p.usetime as usetime FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id LEFT JOIN ' .tablename('mx_shop_service_goods_type_price') . ' p ON o.price_id=p.id LEFT JOIN '.tablename('mx_shop_service_order_refund').' r ON o.id=r.orderid WHERE ' . $condition . ' ORDER BY o.id DESC LIMIT '. (($pindex - 1) * $psize) . ',' . $psize, $paras);

        $total = pdo_fetchcolumn('SELECT count(o.id) FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id WHERE ' . $condition, $paras);

        $money = pdo_fetchcolumn('SELECT sum(o.realprice) as money FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id WHERE ' . $condition, $paras);

        $pager = pagination($total, $pindex, $psize);

        include $this->template('sorder/order/orderlist');

    }

    public function detail(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if(empty($id)){
            show_json(0);
        }

        $item = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_order').' WHERE id=:id', array(':id'=>$id));

        $member = pdo_fetch('SELECT * FROM '.tablename('mx_shop_user').' WHERE id=:id', array(':id'=>$item['uid']));
        if(!empty($member['parentid'])){
            $agent = pdo_fetch('SELECT * FROM '.tablename('mx_shop_user').' WHERE id=:id', array(':id'=>$member['parentid']));
            $agent['commission'] = pdo_fetchcolumn('SELECT money FROM '.tablename('mx_shop_service_commission_log').' WHERE orderid=:id AND uid=:uid', array(':id'=>$id, ':uid'=>$agent['id']));
        }

        $goods = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_goods').' WHERE id=:id', array(':id'=>$item['goods_id']));

        $mendian = pdo_fetch('SELECT d.*, m.id as mid, m.mobile as mobile, m.realname as realname, m.weixin as weixin, m.avatar as avatar FROM '.tablename('mx_shop_mendian').' d LEFT JOIN '.tablename('mx_shop_user').' m ON d.manageid=m.id WHERE d.id=:id', array(':id'=>$item['mendian_id']));

        if($item['status'] > 4){
            $refund = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_order_refund').' WHERE orderid=:id ORDER BY id DESC', array(':id'=>$id));
        }

        if($item['ispay'] == 1){
            $level = array('1'=>'店主', '2'=>'创业者', '3'=>'渠道商', '4'=>'省级运营商', '5'=>'区域运营商', '6'=>'推荐人', '7'=>'消费者');
            $comlog = pdo_fetchall('SELECT l.*,m.id as mid, m.avatar as avatar, m.realname as realname, m.mobile as mobile, m.weixin as weixin, d.mendianname as mendianname FROM '.tablename('mx_shop_service_commission_log').' l LEFT JOIN '.tablename('mx_shop_user').' m ON l.uid=m.id LEFT JOIN '.tablename('mx_shop_mendian').' d ON l.mendianid=d.id WHERE l.orderid=:id', array(':id'=>$id));
            $reslog = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission_reserved_log').' WHERE orderid=:id', array(':id'=>$id));
        }


        include $this->template();

    }
    
    public function ajaxgettotals(){

        global $_W, $_GPC;

        $status1 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=1 AND deleted=0');
        $status2 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=2 AND deleted=0');
        $status3 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=3 AND deleted=0');
        $status4 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=4 AND deleted=0');
        $status5 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=5 AND deleted=0');
        $status6 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE status=6 AND deleted=0');
        $all = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order') . ' WHERE deleted=0');

        $with1 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_withdrawal') . ' WHERE status=1');
        $with2 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_withdrawal') . ' WHERE status=2');
        $with3 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_withdrawal') . ' WHERE status=3');
        $with4 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_withdrawal') . ' WHERE status=4');

        $comment = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order_comment') . ' WHERE deleted=0');
        $comment1 = pdo_fetchcolumn('SELECT count(id) FROM ' . tablename('mx_shop_service_order_comment') . ' WHERE checked=0 AND deleted=0');

        $data['status0'] = intval($status1);
        $data['status1'] = intval($status2);
        $data['status2'] = intval($status3);
        $data['status3'] = intval($status4);
        $data['status5'] = intval($status5);
        $data['status6'] = intval($status6);
        $data['all'] = intval($all);

        $data['with1'] = intval($with1);
        $data['with2'] = intval($with2);
        $data['with3'] = intval($with3);
        $data['with4'] = intval($with4);

        $data['comment'] = intval($comment);
        $data['comment1'] = intval($comment1);

        echo json_encode($data);
    }

}
